/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.game.map;

import deatax.RNG;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author pson
 */
public class MapController {
    
    private Map map;
    
    public static List<Integer> NORMAL = null;
    //Used when y-value is even
    public final static int[] X_EVEN = new int[]{0, 1, -1, 1, 0, 1};
    public final static int[] Y_NEIGH = new int[]{-1, -1, 0, 0, 1, 1};
    
    //Ued when y-value is odd
    public final static int[] X_ODD = new int[]{-1, 0, -1, 1, -1, 0};

    public void setMap(Map map) {
        this.map = map;
    }
    
    public List<Integer> getNormalOrder() {
        if (NORMAL == null) {
            NORMAL = new ArrayList<Integer>();
            for (int i = 0; i < 6; ++i) {
                NORMAL.add(i);
            }
        }
        return NORMAL;
    }
    
    
    
    public LinkedList<Tile> getNeighbourList(Tile t) {
        return getNeighbourList(t.getX(), t.getY());
    }
    
    public LinkedList<Tile> getNeighbourList(int x, int y) {
        return getNeighbourList(x, y, getNormalOrder());
    }
    
    public LinkedList<Tile> getNeighbourListRandom(Tile t) {
        return getNeighbourListRandom(t.getX(), t.getY());
    }
    
    public LinkedList<Tile> getNeighbourListRandom(int x, int y) {
        List<Integer> order = new ArrayList<Integer>(NORMAL);
        Collections.shuffle(order);
        return getNeighbourList(x, y, order);
    }
    
    public LinkedList<Tile> getNeighbourList(int x, int y, List<Integer> order) {
        int[] offx;
        int[] offy = Y_NEIGH;
        if (y % 2 == 0) {
            offx = X_EVEN;
        } else {
            offx = X_ODD;
        }
        LinkedList<Tile> tiles = new LinkedList<Tile>();
        for (int i = 0; i < 6; ++i) {
            int xn = x + offx[order.get(i)];
            int yn = y + offy[order.get(i)];
            if (!(xn < 0 || xn >= map.getWidth() || yn < 0 || yn >= map.getHeight())) {
                Tile t = map.getTile(xn, yn);
                if (t != null) {
                    tiles.add(t);
                }
            }
        }
        return tiles;
    }
    
    public Tile createRandomNeighbour(int x, int y) {
        List<Integer> order = new ArrayList<Integer>(NORMAL);
        Collections.shuffle(order);
        return createNeighbour(x, y, order);
    }
    
    public Tile createNeighbour(int x, int y, List<Integer> order) {
        int[] offx;
        int[] offy = Y_NEIGH;
        if (y % 2 == 0) {
            offx = X_EVEN;
        } else {
            offx = X_ODD;
        }
        for (int i = 0; i < 6; ++i) {
            int xn = x + offx[order.get(i)];
            int yn = y + offy[order.get(i)];
            if (!(xn < 0 || xn >= map.getWidth() || yn < 0 || yn >= map.getHeight())) {
                Tile t = map.getTile(xn, yn);
                if (t == null) {
                    t = new Tile(xn, yn);
                    return t;
                }
            }
        }
        return null;
    }
    
    public Tile getRandomTile() {
        return map.getRandomTile();
    }
    
    public void addTile(Tile t) {
        map.setTile(t);
    }
}
